﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Charts;
using DAL.Models;
using DAL.BAL;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for ReportVacancies.xaml
    /// </summary>
    public partial class ReportVacancies : Window
    {
   
        public ReportVacancies()
        {
            InitializeComponent();

            deDateFrom.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            deDateTo.DateTime = DateTime.Now;

            if (Business.currentUser.RoleID != 1)
            {
                cbReportType.SelectedIndex = 1;

                cbReportType.IsEnabled = false;
            }
            else
            {
                cbReportType.SelectedIndex = 0;
            }

            deDateFrom.EditValueChanged += deDateFrom_EditValueChanged;

            deDateTo.EditValueChanged += deDateFrom_EditValueChanged;

            cbReportType.SelectedIndexChanged += cbType_SelectedIndexChanged;

            Refresh();
        }

        public void Refresh()
        {
            chart.BeginInit();

            (chart.Diagram as XYDiagram2D).AxisY.CustomLabels.Clear();

            List<ReportPOCO> reportValues = null;

            if (cbReportType.SelectedIndex == 0)
                reportValues = Business.GetVacanciesReportComulative (deDateFrom.DateTime, deDateTo.DateTime);
            else
                reportValues = Business.GetVacanciesReportPerDay(deDateFrom.DateTime, deDateTo.DateTime);

            (chart.Diagram as XYDiagram2D).Series[0].DataSource = reportValues;

            (chart.Diagram as XYDiagram2D).Series[0].ArgumentDataMember = "Date";

            (chart.Diagram as XYDiagram2D).Series[0].ValueDataMember = "Value";

            foreach (var rp in reportValues.OrderBy(c => c.Value).GroupBy(c => c.Value).Select(c => new
            {
                Value = c.Key
            }))
            {
                (chart.Diagram as XYDiagram2D).AxisY.CustomLabels.Add(new CustomAxisLabel(rp.Value, rp.Value.ToString()));
            }

            chart.EndInit();

            chart.Animate();
        }

        private void deDateFrom_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            Refresh();
        }

        private void cbType_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            Refresh();
        }


        private void bRefresh_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Refresh();
        }

    }
}
